package vashion.azeroth.back.controller.tran;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import vashion.azeroth.back.controller.base.BaseController;
import vashion.azeroth.back.core.context.BackUserContext;
import vashion.azeroth.common.common.check.FormatCheck;
import vashion.azeroth.contant.GenericErrorEnum;
import vashion.azeroth.core.manager.personal.PersonalMemberManager;
import vashion.azeroth.core.personal.pojo.PersonalMember;
import vashion.azeroth.core.result.ApiResult;
import vashion.azeroth.core.service.PersonalMemberService;

/**
 * 用户管理页面
 */
@Controller
@RequestMapping("/back/member")
public class UserManagerController extends BaseController {

	@Autowired
	private PersonalMemberService<PersonalMember> personalMemberService;
	@Autowired
	protected BackUserContext userContext;

	@Autowired
	private PersonalMemberManager personalMemberManager;

	/**
	 * 登陆
	 *
	 * @param member
	 * @return
	 */
	@RequestMapping("getMemberList.htm")
	@ResponseBody
	public ApiResult<List<PersonalMember>> getMemberList(@ModelAttribute("member") PersonalMember member,
			HttpServletRequest request) {

		List<PersonalMember> memberList = personalMemberManager.find(member);
		return ApiResult.newSuccessResult(memberList);
	}

	/**
	 * 注册接口
	 * 
	 * @param member
	 * @return
	 */
	@RequestMapping("generateProviderAccount.htm")
	@ResponseBody
	public ApiResult<PersonalMember> generateProviderAccount(@ModelAttribute("member") PersonalMember member,
			HttpServletRequest request) {

		if (StringUtils.isBlank(member.getMemberName())) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(),
					"用户名" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
		}

		if (!FormatCheck.isMobile(member.getMemberName())) {
			return ApiResult.newErrorResult(GenericErrorEnum.PARAM_FORMAT_ERROR.getErrorCode(),
					"用户名" + GenericErrorEnum.PARAM_FORMAT_ERROR.getErrorMessage());
		}

		if (StringUtils.isBlank(member.getMemberPwd())) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(),
					"密码" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
		}

		if (!FormatCheck.isLegalPassword(member.getMemberPwd())) {
			return ApiResult.newErrorResult(GenericErrorEnum.PARAM_FORMAT_ERROR.getErrorCode(),
					"密码" + GenericErrorEnum.PARAM_FORMAT_ERROR.getErrorMessage());
		}
		PersonalMember generatedProvider = personalMemberManager.generateProviderAccount(member,
				getLoginUserId(request));
		if (null != generatedProvider && null != generatedProvider.getMemberId()
				&& 0 < generatedProvider.getMemberId()) {
			return ApiResult.newSuccessResult(generatedProvider);
		}

		return ApiResult.newErrorResult(GenericErrorEnum.SYSTEM_ERROR.getErrorCode(),
				GenericErrorEnum.SYSTEM_ERROR.getErrorMessage());
	}

}
